package com.shark.agentweb;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.shark.agentweb.commom.FragmentKeyDown;


/**
 * @author: guojr
 * @date: 2019/12/5
 */
public class SharkWebActivity extends AppCompatActivity {
    private int keyType = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        keyType = 0;

        handleIntent();

    }

    private void handleIntent() {

        if (getIntent().hasExtra(SharkWeb.KEY_TYPE)){
            keyType = getIntent().getIntExtra(SharkWeb.KEY_TYPE, 0);
        }

        if (getIntent().hasExtra(SharkWeb.KEY_URL)){
            String url =  getIntent().getStringExtra(SharkWeb.KEY_URL);
            if (TextUtils.isEmpty(url)){
                Toast.makeText(this, "加载数据出错！！！", Toast.LENGTH_SHORT).show();
                this.finish();
            }else{
                loadFragment(keyType, url);
            }
        }
    }

    private SharkWebFragment mAgentWebFragment;
    /**
     * loadFragment
     * @param url
     */
    private void loadFragment(int keyType, String url) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Bundle mBundle = null;

        switch (keyType){
            /*默认界面*/
            case SharkWeb.FLAG_GUIDE_IN_FRAGMENT_DEFAULT:
                ft.add(R.id.container_frame_layout,
                        mAgentWebFragment = SharkWebFragment.getInstance(mBundle = new Bundle()),
                        SharkWebFragment.class.getName());
                mBundle.putString(SharkWeb.KEY_URL, url);
                break;
            default:
                break;
        }
        ft.commit();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        SharkWebFragment mAgentWebFragment = this.mAgentWebFragment;
        if (mAgentWebFragment != null) {
            FragmentKeyDown mFragmentKeyDown = mAgentWebFragment;
            if (mFragmentKeyDown.onFragmentKeyDown(keyCode, event)) {
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }

        return super.onKeyDown(keyCode, event);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
